package com.data.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@SuppressWarnings("ALL")
@Slf4j
@Component
public class DeviceClientUtil {


    private static String rebootUrl;

    private static String remoteOpenDoorUrl;

    @Value("${deviceInteface.remoteOpen:}")
    public void setRemoteOpenDoorUrl(String remoteOpenDoorUrl) {
        DeviceClientUtil.remoteOpenDoorUrl = remoteOpenDoorUrl;
    }

    @Value("${deviceInteface.reboot:}")
    public void setRebootUrl(String addUserUrl) {
        DeviceClientUtil.rebootUrl = addUserUrl;
    }

    public static Boolean doPostJson(String uniqueNo) {

        Boolean flag = false;
        String str = "{\"serialNo\":\"1000\"}";
        str = str.replace("1000",uniqueNo);

        String result = HttpClientUtil.doPostJson(rebootUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

    public static Boolean doPostOpenDoorJson(String uniqueNo) {

        Boolean flag = false;
        String str = "{\"serialNo\":\"1000\"}";
        str = str.replace("1000",uniqueNo);

        String result = HttpClientUtil.doPostJson(remoteOpenDoorUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

    public static Boolean doPost(String uniqueNo,String password) {

        Boolean flag = false;
        String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",uniqueNo).replace("870830",password);

        String result = HttpClientUtil.doPostJson(rebootUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

}
